package com.example.futureai.service.impl;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.futureai.config.propertise.VectorStorePropertiesConfig;
import com.example.futureai.constants.SpringAIConst;
import com.example.futureai.dto.DocSgtDto;
import com.example.futureai.mapper.chat.ChatDocumentRecordMapper;
import com.example.futureai.mapper.chat.DocSgtMapper;
import com.example.futureai.po.ChatDocumentRecord;
import com.example.futureai.po.DocSgtPo;
import com.example.futureai.service.RedisService;
import com.example.futureai.service.ZskService;
import com.example.futureai.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.filter.Filter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: ZskService
 * @Description:
 * @Author: YourName
 * @Date: 2025/3/6
 */
@Service
@Slf4j
public class ZskServiceImpl implements ZskService {
    @Autowired
    private ChatDocumentRecordMapper chatDocumentRecordMapper;
    @Autowired
    private DocSgtMapper docSgtMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private VectorStorePropertiesConfig vpc;


    public void addZskJl(List<Document> newDocuments, String docName, String docHash) {
        String currentUserId = TokenUtil.getCurrentUserId();
        //插一条文档记录
        ChatDocumentRecord chatDocumentRecord = new ChatDocumentRecord();
        chatDocumentRecord.setUsername(currentUserId);
        chatDocumentRecord.setDocId(docHash);
        chatDocumentRecord.setDocName(docName);
        chatDocumentRecordMapper.insert(chatDocumentRecord);
        //插入分段记录
        newDocuments.stream().forEach(document -> {
            DocSgtPo docSgtPo = new DocSgtPo();
            docSgtPo.setDocId(docHash);
            docSgtPo.setSgtId(document.getId());
            docSgtPo.setContent(document.getText());
            docSgtMapper.insert(docSgtPo);
        });
    }
}
